/**
 * 日志管理系统，暂时只支持写日志
 * 存储，上传，待定
 */
//------------------------------------------logs-----------------------------------------
module.exports={
    styles : {
        'bold'          : ['\x1B[1m','\x1B[22m'],
        'italic'        : ['\x1B[3m','\x1B[23m'],
        'underline'     : ['\x1B[4m','\x1B[24m'],
        'inverse'       : ['\x1B[7m','\x1B[27m'],
        'strikethrough' : ['\x1B[9m','\x1B[29m'],
        'white'         : ['\x1B[37m','\x1B[39m'],
        'grey'          : ['\x1B[90m','\x1B[39m'],
        'black'         : ['\x1B[30m','\x1B[39m'],
        'blue'          : ['\x1B[34m','\x1B[39m'],
        'cyan'          : ['\x1B[36m','\x1B[39m'],
        'green'         : ['\x1B[32m','\x1B[39m'],
        'magenta'       : ['\x1B[35m','\x1B[39m'],
        'red'           : ['\x1B[31m','\x1B[39m'],
        'yellow'        : ['\x1B[33m','\x1B[39m'],
        'whiteBG'       : ['\x1B[47m','\x1B[49m'],
        'greyBG'        : ['\x1B[49;5;8m','\x1B[49m'],
        'blackBG'       : ['\x1B[40m','\x1B[49m'],
        'blueBG'        : ['\x1B[44m','\x1B[49m'],
        'cyanBG'        : ['\x1B[46m','\x1B[49m'],
        'greenBG'       : ['\x1B[42m','\x1B[49m'],
        'magentaBG'     : ['\x1B[45m','\x1B[49m'],
        'redBG'         : ['\x1B[41m','\x1B[49m'],
        'yellowBG'      : ['\x1B[43m','\x1B[49m']
    },
    _debug:false,
    _logger:null,
    logtime : new Date().getTime(),
    /**
     */
    getLogTimeString:function(){
        var date=new Date();
        return (date.getMonth()+1)+"/"+(date.getDate())+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+"-"+date.getMilliseconds();
    },
    init:function(debug){
        this._debug=debug;
    },
    setLogger(logger){
        this._logger=logger;
    },
    log:function(msg,color){
        if(this._logger)
            this._logger(msg,color);
        if(color)
            console.log(color[0]+msg+color[1]);
        else
            console.log(msg);
    },
    logP:function(msg)    {
        if(this._debug)
            this.log(this.getLogTimeString()+" protocol::"+msg,this.styles.blue);
    },
    logM:function(msg){
        // if(this._debug)
            this.log(this.getLogTimeString()+" message::"+msg,this.styles.green);
    },
    logW:function(msg){
        // if(this._debug)
            this.log(this.getLogTimeString()+" warning::"+msg,this.styles.yellow);
    },
    logE:function(msg)//错误
    {
        this.log(this.getLogTimeString()+" error::"+msg,this.styles.red);
        // var url="http://192.168.8.103/log.php?id=123&name="+logtime+".log&data="+msg;
        // var xhr = cc.loader.getXMLHttpRequest();
        // xhr.open("GET", url, true);
        // xhr.send();
    },
    logObj:function(obj,desstr)
    {
        if(!this._debug)
            return;
        if(!desstr)
            desstr="-";
        else
            desstr=desstr+"-"
        for (var key in obj)
        {
            var str=typeof obj[key];
            switch(str)
            {
                case "function":
                    this.logM(desstr+key+":"+"function");
                    break;
                case "number":
                    this.logM(desstr+key+":"+obj[key]);
                    break;
                case "string":
                    this.logM(desstr+key+":"+obj[key]);
                    break;
                default:
                    this.logM(desstr+key+":");
                    this.logObj(obj[key],desstr)
                    break;
            }
        }
    },
    logStact:function(arg){
        try {
            throw new Error();
        } catch (e) {
            logW("\n"+e.stack);
        }
    }
}
